package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.ShopMapper;
import com.chixing.pojo.*;
import com.chixing.service.SearchService;
import com.chixing.util.ServerResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private CarMapper carMapper;

    /**
     * 模糊查询
     *
     * @param keyword 前端传回的数据
     * @param pageNum 当前页面是第几页
     * @return 返回ServerResult
     */
    @Override
    public ServerResult getByKeyword(String keyword, Integer pageNum) {

        //先判断用户是否输入的是有关于店铺的信息、店铺名、店铺地址、店铺所处区域
        ShopExample shopExample = new ShopExample();
        shopExample.or().andShopNameLike("%" + keyword + "%");
        shopExample.or().andShopAddressLike("%" + keyword + "%");
        shopExample.or().andShopAreaLike("%" + keyword + "%");

        List<Shop> shopList = shopMapper.selectByExample(shopExample);

        PageHelper.startPage(pageNum, 3);
        List<Car> carList = new ArrayList<>();
        CarExample example = new CarExample();
        //如果店铺列表有数据，则根据店铺将车辆信息取出，如果没有则根据车辆信息直接取出车辆
        if (shopList != null && shopList.size() > 0) {
            for (Shop shop : shopList) {
                example.createCriteria().andShopIdEqualTo(shop.getShopId());
                List<Car> cars = carMapper.selectByExample(example);
                for (Car car : cars) {
                    System.out.println("car：" + car);
                    carList.add(car);
                }
            }
        } else {
            example.or().andCarBrandLike("%" + keyword + "%");
            example.or().andCarNameLike("%" + keyword + "%");
            example.or().andCarTypeLike("%" + keyword + "%");

            carList = carMapper.selectByExample(example);
        }

        PageInfo pageInfo = new PageInfo<>(carList);

        if (carList != null && carList.size() > 0) {
            return ServerResult.getSussess(pageInfo);
        } else {
            return ServerResult.getFail("所查询的车辆不存在");
        }
    }

    @Override
    public ServerResult getAll(Integer shopId, Integer pageNum) {

        PageHelper.startPage(pageNum, 3);
        if (shopId == null) {
            shopId = 20001;
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        CarExample example = new CarExample();
        example.createCriteria().andShopIdEqualTo(shopId);
        example.setOrderByClause("car_daily_price desc");
        List<Car> carList = carMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(carList);

        if (carList != null && carList.size() > 0) {
            return ServerResult.getSussess(pageInfo);
        }

        return ServerResult.getFail(null);
    }

    @Override
    public ServerResult getByMoreKeyword(String carType, String carBrand, Float minCarDailyPrice, Float maxCarDailyPrice, String carShiftType, Integer carSeatNumber, Integer pageNum) {
        PageHelper.startPage(pageNum, 3);

        CarExample example = new CarExample();
        CarExample.Criteria criteria = example.createCriteria();

        System.out.println("carType:" + carType);


        if (!carType.equals("-1")) {
            criteria.andCarTypeEqualTo(carType);
        }
        if (!carBrand.equals("-1")) {
            criteria.andCarBrandEqualTo(carBrand);
        }
        if (minCarDailyPrice != -1) {
            criteria.andCarDailyPriceGreaterThan(minCarDailyPrice);
        }
        if (maxCarDailyPrice != -1) {
            criteria.andCarDailyPriceLessThan(maxCarDailyPrice);
        }
        if (carSeatNumber != -1) {
            criteria.andCarSeatCountEqualTo(carSeatNumber);
        }

        example.setOrderByClause("car_daily_price desc");
//        example.setOrderByClause("car_daily_price desc");
        List<Car> carList = carMapper.selectByExample(example);
        for (Car car : carList) {
            System.out.println(car);
        }
        PageInfo pageInfo = new PageInfo<>(carList);

        if (carList != null && carList.size() > 0) {
            return ServerResult.getSussess(pageInfo);
        }

        return ServerResult.getFail(null);

    }

    @Override
    public ServerResult getShopByCity(String shopCity) {
        ShopExample example = new ShopExample();
        example.createCriteria().andShopAddressLike("%" + shopCity + "%");
        List<Shop> shopList = shopMapper.selectByExample(example);
        if (shopList != null && shopList.size() > 0) {
            return ServerResult.getSussess(shopList);
        }
        return ServerResult.getFail(null);
    }

    @Override
    public ServerResult getByCarAndShop(Integer shopId, Integer pageNum, Integer carId) {
        PageHelper.startPage(pageNum, 3);
        if (shopId == null) {
            shopId = 20001;
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        CarExample example = new CarExample();
        example.createCriteria().andShopIdEqualTo(shopId).andCarIdEqualTo(carId);
        List<Car> carList = carMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(carList);
        if (carList != null && carList.size() > 0) {
            return ServerResult.getSussess(pageInfo);
        }
        return ServerResult.getFail(null);
    }

}
